package com.strato.osgi.repository.v1_0_0.activator;

import java.io.File;
import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.strato.base.env.v1_0_0.GlobalEnvironment;
import com.strato.osgi.repository.v1_0_0.Repository;
import com.strato.osgi.repository.v1_0_0.impl.FileSystemRepository;

public class Activator implements BundleActivator {

	private static BundleContext context;

	@Override
	public void start(BundleContext bundleContext) throws Exception {
		context=bundleContext;
		String rootDir=bundleContext.getProperty("strato.osgi.repository");
		if(rootDir==null){
			String catalinaHome=System.getProperty("catalina.home");
			File dir=new File(catalinaHome);
			File repDir=new File(dir,"/plugins");
			rootDir=repDir.getAbsolutePath();
			GlobalEnvironment.getInstance().set("strato.osgi.repository",rootDir);
		}
		System.out.println("Using reposiroty:"+rootDir);
		FileSystemRepository repository=new FileSystemRepository(rootDir);
		repository.rescan();
		Dictionary<String, String> dict=new Hashtable<String, String>();
		dict.put("id", "fileSystemRepository");
		bundleContext.registerService(Repository.class, repository, dict);
	}

	@Override
	public void stop(BundleContext bundleContext) throws Exception {
		context=null;
	}

	public static BundleContext getContext() {
		return context;
	}
	
}
